﻿using Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Git
{
    partial class git
    {

        private Boolean commot = false;

        private string filepat = "";

        void p_gitaddcommit_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                if (!commot) {
                    if (Regex.IsMatch(e.Data, "git commit -a -m "))
                    {
                        commot = true;
                    }
                }
                Console.WriteLine(e.Data);
            }
        }

        public Boolean gitaddcommit()
        {
            //是否是Git
            if (GlobalDirectory.GitRemote == true)
            {
                gitconfig gc = git.gitexelist.Find(x => x.Value == "GITProductURL");
                git g = new git();
                if (gc != null)
                {
                    if (gc.Key != null)
                    {
                        if (Directory.Exists(gc.Key))
                        {
                            return g.gitaddcommit(gc.Key);
                        }
                    }
                }
            }
            return false;
        }

        private Boolean gitaddcommit(string file)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.OutputDataReceived += p_gitaddcommit_OutputDataReceived;
                p.Start();
                git.fetchcodelist.Clear();
                p.StandardInput.WriteLine(@"cd """ + file + @"""");
                //p.StandardInput.WriteLine("git add .");
                p.StandardInput.WriteLine(@"git commit -a -m """ + DateTime.Now.ToString() + @"""");               
                p.StandardInput.WriteLine("exit");
                p.BeginOutputReadLine();
                p.WaitForExit();
                p.Close();
                return commot;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
